#!/usr/bin/env python
# coding=utf-8

import os
import sys
import re
import codecs
import pickle
import cPickle
from OffsetDump import KiwiDataManager
from VrDicCheck import *
import time

KIWI_INDEX_FILE = r'\\solar\data$\10Model\JB014\090213_SP2\INDEXDAT.KWI'
VR_DIC_FOLDER = r'\\solar\data$\10Model\JB014\090213_SP2\VoiceData\CHN\80\VOICE'
# set log
import CheckLog
log = CheckLog.get_file_log(r'check.log')


def check():
	print('Loading Kiwi Data ...')
	DicItem._folder = VR_DIC_FOLDER
	dumpFilename = KIWI_INDEX_FILE.replace('\\', '_').replace('$', '_').replace(':', '_')
	if os.path.isfile(dumpFilename):
		kdm = cPickle.load(open(dumpFilename, 'r'))
	else:
		kdm = KiwiDataManager(KIWI_INDEX_FILE)
		f = open(dumpFilename, 'w')
		cPickle.dump(kdm, f)
		f.close()

	DicItem._kiwiDataManager = kdm
	print('Checking ...')
	rt = DicRootItem()
	rt.check_recursively()
	print('Check Finished')

def main():
	if len(sys.argv) != 3:
		print('Usage:')
		print(r'python check.py [KiwiIndexFilepath] [VrDicPath]')
		print(r'Example: python check.py \\solar\data$\10Model\JB014\090213_SP2\INDEXDAT.KWI \\solar\data$\10Model\JB014\090213_SP2\VocieData\CHN\80\VOICE')
		sys.exit(0)
	global KIWI_INDEX_FILE, VR_DIC_FOLDER
	KIWI_INDEX_FILE = sys.argv[1]
	VR_DIC_FOLDER = sys.argv[2]
	try:
		import psyco
		psyco.full()
	except:
		print('Warning: Using psyco failed. It will still work correctly, but much more slowly.')
	check()

if __name__ == '__main__':
	main()

